package vn.edu.fpt.xml.back.batch;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import vn.edu.fpt.xml.back.reader.exceptions.ReadException;
import vn.edu.fpt.xml.back.services.news.NewsReaderService;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;

/**
 * Created by hiephn on 2014/04/24.
 */
@Component
public class CommandHandler {

    @Resource(name = "newsService")
    protected NewsReaderService newsReaderService;

    public static void main(String[] args) {
        CommandHandler commandHandler = new CommandHandler();
        ConfigurableApplicationContext applicationContext = null;

        try {
            applicationContext = new ClassPathXmlApplicationContext("/xn-back-batch-context.xml");
            applicationContext.registerShutdownHook();
            applicationContext.getBeanFactory().autowireBeanProperties(commandHandler, AutowireCapableBeanFactory.AUTOWIRE_NO, false);

            commandHandler.processCommand(args);
        } finally {
            if (applicationContext != null) {
                applicationContext.close();
            }
        }
    }

    protected void processCommand(String[] args) {
        try {
            newsReaderService.processService();
        } catch (ReadException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
